﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Net.Sockets;
using System.IO;

namespace LolBot
{
    class LolBotCommandHandler
    {
        private static const String basicCommands = "";
        private static const String opCommands = "";

        private StreamReader reader;
        private StreamWriter writer;
        private IRCConfig config;

        private bool iShouldSing = false;

        public LolBotCommandHandler(StreamReader reader, StreamWriter writer, IRCConfig config)
        {
            this.reader = reader;
            this.writer = writer;
            this.config = config;
        }

        //BASIC COMMANDS
        public void OnPing(Object source, LolBotEvent e)
        {
            Console.WriteLine("Got Ping " + e.PingMessage + 
                ", responding with PONG " + e.PingMessage);
            writer.WriteLine("PONG " + e.PingMessage);
            writer.Flush();
        }

        public void OnJoin(Object source, LolBotEvent e)
        {
        }

        public void OnKick(Object source, LolBotEvent e)
        {
            try 
            {
			    Thread.Sleep(2000);
                writer.WriteLine("JOIN " + config.channel);
				writer.Flush();
		    } 
            catch
            {
		    }
        }

        //PARSED WORDS FROM MESSAGE
        public void OnRendezvous(Object source, LolBotEvent e)
        {
            SendChannelMessage(e.SourceNick + ": Menar du inte \"Déja vu\"?");
        }

        public void OnMesserschmidt(Object source, LolBotEvent e)
        {
            SendChannelMessage(e.SourceNick + ": \"Besserwisser\" heter det faktiskt!");
        }

        //USER COMMANDS
        public void OnExit(Object source, LolBotEvent e)
        {
            writer.WriteLine("QUIT " + config.channel);
            writer.Flush();
        }

        public void OnOp(Object source, LolBotEvent e)
        {
            Op(e.Arguments);
        }

        public void OnAddOp(Object source, LolBotEvent e)
        {
            foreach (String nick in e.Arguments)
            {
                SendChannelMessage("Added " + nick + " to the Op list!");
            }
        }

        public void OnRemoveOp(Object source, LolBotEvent e)
        {
            foreach (String nick in e.Arguments)
            {
                SendChannelMessage("Removed " + nick + " from the Op list!");
            }
        }

        public void OnAddBadWord(Object source, LolBotEvent e)
        {
            foreach (String word in e.Arguments)
            {
                SendChannelMessage("Added " + word + " to the bad words list!");
            }
        }

        public void OnRemoveBadWord(Object source, LolBotEvent e)
        {
            foreach (String word in e.Arguments)
            {
                SendChannelMessage("Removed " + word + " from the bad words list!");
            }
        }

        public void OnIgnore(Object source, LolBotEvent e)
        {
            foreach (String nick in e.Arguments)
            {
                SendChannelMessage("Added " + nick + " to the ignore list!");
            }
        }

        public void OnUnignore(Object source, LolBotEvent e)
        {
            foreach (String nick in e.Arguments)
            {
                SendChannelMessage("Removed " + nick + " from the bad words list!");
            }
        }

        public void OnHelp(Object source, LolBotEvent e)
        {
            if(!isOnOpList(e.SourceNick))
            {
                SendChannelMessage(e.SourceNick + ": You are not on the Op list, so these commands are available for you:");
                SendChannelMessage(basicCommands);
            }
            else
            {
                SendChannelMessage(e.SourceNick + ": You are on the Op list, so these commands are available for you:");
                SendChannelMessage(opCommands);
            }
        }

        public void OnDate(Object source, LolBotEvent e)
        {
            SendChannelMessage(e.SourceNick + ": " + 
                DateTime.Now.Date.ToShortDateString());
        }

        public void OnSing(Object source, LolBotEvent e)
        {
            iShouldSing = true;
            SendChannelMessage("I should sing here...");
        }

        public void OnStopSinging(Object source, LolBotEvent e)
        {
            iShouldSing = false;
        }

        public void OnSay(Object source, LolBotEvent e)
        {
            String message = "";
			foreach(String word in e.Arguments) {
				message += word + " ";
			}
            SendChannelMessage(message);
        }

        public void OnMe(Object source, LolBotEvent e)
        {
            String message = "";
			foreach (String word in e.Arguments)
				message += " " + word;
			SendChannelMessage("\001ACTION" + message + "\001");
        }

        // HELPER METHODS
        private void SendChannelMessage(String message)
        {
            writer.WriteLine("PRIVMSG " + config.channel + " :" + message);
            writer.Flush();
        }

        private void Op(List<String> opNicks)
        {
            String os = "";
            String nicks = "";

            foreach (String opNick in opNicks)
            {
                nicks += opNick + " ";
                os += "o";
            }

            writer.WriteLine("MODE " + config.channel + " +" + os + " " + nicks);
			writer.Flush();
        }

        private bool isOnOpList(String nick)
        {
            return true; //TODO: Fix!
        }
    }
}
